package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.dto.AdvposPage;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.exception.ConsumerException;
import com.itheima.ydd.mapper.AdvposMapper;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author maorf
 * @version 1.0
 */
@Service
public class AdvposServiceImpl extends ServiceImpl<AdvposMapper, Advpos> implements AdvposService {
    @Autowired
    AdvimgService advimgService;
    @Override
    public AdvposPage getPage(String advposdesc, String advposname, Integer page, Integer limit) {
        LambdaQueryWrapper<Advpos> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(advposname),Advpos::getAdvposname,advposname)
                .like(!StringUtils.isEmpty(advposdesc),Advpos::getAdvposdesc,advposdesc);
        Page<Advpos> iPage = new Page<>(page,limit);
        page(iPage,qw);
        Page<AdvposDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(iPage,dtoPage,"records");
        List<Advpos> advposList = iPage.getRecords();

        List<AdvposDto> advposDtoList=advposList.stream().map((item)->{
            AdvposDto advposDto = new AdvposDto();
            advposDto.setId(item.getId());
            advposDto.setAdvposdesc(item.getAdvposdesc());
            advposDto.setAdvposname(item.getAdvposname());
            advposDto.setAdvpossize(item.getAdvpossize());
            advposDto.setDefault_data(item.getDefault_data());
            LambdaQueryWrapper<Advimg> queryWrapper  = new LambdaQueryWrapper<>();
            queryWrapper.eq(Advimg::getAdvimgpos,item.getId());
            int count = advimgService.count(queryWrapper);
            advposDto.setAdvposnum(count);
            return advposDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(advposDtoList);
        //设置page数据为自定义
        AdvposPage advposPage = new AdvposPage();
        advposPage.setCount((int)iPage.getTotal());
        advposPage.setCurrentPage((int) iPage.getCurrent());
        advposPage.setPageSize((int) iPage.getSize());
        advposPage.setTotalPages((int) iPage.getPages());
        advposPage.setData(dtoPage.getRecords());

        return advposPage;
    }

    @Override
    public void remove(String id) {
        LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Advimg::getAdvimgpos,id);
        List<Advimg> list = advimgService.list(queryWrapper);
        if (!ObjectUtils.isEmpty(list)){
            throw new ConsumerException("存在广告图,无法删除");
        }
        removeById(id);
    }
}
